<template>
  <a-modal
    :open="open"
    width="660px"
    :title="rowData?.lsh ? '修改盘点单' : '新增盘点单'"
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    centered
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '80px' } }">
      <a-form-item label="盘点仓库" class="w-1/2" v-bind="validateInfos.ckid">
        <SelectWarehouse v-model:value="formState.ckid" class="!w-52" />
      </a-form-item>
      <a-form-item label="" class="w-1/2">
        <a-switch v-model:checked="formState.zeroFlag" :un-checked-value="0" :checked-value="1" class="mx-1" />包含零库存项目
        <a-switch v-model:checked="formState.isHasDisable" :un-checked-value="1" :checked-value="0" class="mx-1" />包含停用项目
      </a-form-item>
      <a-form-item label="盘点人" class="w-1/2" v-bind="validateInfos.checkEmpidStr">
        <SelectTable v-model:value="formState.checkEmpidStr" type="员工" class="!w-52" @select="onSelectEmp" />
      </a-form-item>
      <a-form-item label="制单人" class="w-1/2">
        {{ empName }}
      </a-form-item>
      <a-form-item label="备注" class="w-full mb-3">
        <a-textarea v-model:value="formState.remarks" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import useUserStore from '@/store/user'
import SelectWarehouse from '@/components/SelectWarehouse'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])

const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  zeroFlag: 0,
  isHasDisable: 0,
  lsh: '',
  checkEmpid: '',
  checkEmpidStr: '',
  ckid: '',
  ckmc: '',
  remarks: ''
})
const rulesState = ref({
  ckid: [{ required: true, message: '请选择盘点仓库' }],
  checkEmpidStr: [{ required: true, message: '请选择盘点人' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
// const formItemLayout = {
const { empName } = useUserStore()

watchEffect(() => {
  if (props.open) {
    formState.value = { zeroFlag: 0, isHasDisable: 0, ...props.rowData }
  }
})
const onCancel = () => {
  resetFields()
  emit('update:open', false)
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/drugStorage/psiform/doInventorySealing', formState.value)
  if (code !== 0) message.error(msg)
  message.success(msg)
  emit('ok')
  onCancel()
}
const onSelectEmp = (emp) => {
  formState.value.checkEmpid = emp.empId
  formState.value.checkEmpidStr = emp.empName
}
</script>
<style lang="less" scoped></style>
